package org.kotboot.kit;

import com.alibaba.fastjson.util.TypeUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * jisong.sun 2017/9/18 15:39
 */
public class NumKit {

    public static final Pattern NUM_POSITIVE = Pattern.compile("^[1-9](\\d+)?$");
    public static final Pattern NUM_INTEGER = Pattern.compile("^\\d+$");

    /**
     * 判断是否是[正]整数，不包括0
     * <p>方法名 : isPositive</p>
     * @author jisong.sun
     * @date 2017年5月27日 下午1:08:43
     * @version 1.0
     * @param o
     * @return boolean
     * <p>修改履历：</p>
     */
    public static boolean isPositive(Object o){
        if(null == o) return false;
        if(!NUM_POSITIVE.matcher(o.toString()).find()){
            return false;
        }
        return true;
    }

    public static boolean notPositive(Object o){
        return !isPositive(o);
    }

    /**
     * 判断是否是整数，包括0
     * <p>方法名 : isInteger</p>
     * @author jisong.sun
     * @date 2017年5月27日 下午1:12:20
     * @version 1.0
     * @param o
     * @return boolean
     * <p>修改履历：</p>
     */
    public static boolean isInteger(Object o){
        if(null == o) return false;
        if(!NUM_INTEGER.matcher(o.toString()).find()){
            return false;
        }
        return true;
    }

    public static boolean notInteger(Object o){
        return !isInteger(o);
    }

    public static String format(Object num,String pattern){
        DecimalFormat df = new DecimalFormat();
        df.applyPattern(pattern);
        return df.format(num);
    }

    public static Integer getInteger(Object src){
        return TypeUtils.castToInt(src);
    }

    public static Integer getIntegerSafe(Object src){
        try {
            return TypeUtils.castToInt(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static int getInt(Object src){
        return getInt(src,0);
    }

    public static int getInt(Object src,int defaultValue){
        Integer integerSafe = getIntegerSafe(src);
        return null == integerSafe?defaultValue:integerSafe;
    }



    public static Long getLong(Object src){
        return TypeUtils.castToLong(src);
    }

    public static Long getLongSafe(Object src){
        try {
            return TypeUtils.castToLong(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static long getLongValue(Object src){
        return getLongValue(src,0L);
    }

    public static long getLongValue(Object src,long defaultValue){
        Long longSafe = getLongSafe(src);
        return null == longSafe?defaultValue:longSafe;
    }


    public static Float getFloat(Object src){
        return TypeUtils.castToFloat(src);
    }

    public static Float getFloatSafe(Object src){
        try {
            return TypeUtils.castToFloat(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static float getFloatValue(Object src){
        return getFloatValue(src,0f);
    }

    public static float getFloatValue(Object src,float defaultValue){
        Float floatSafe = getFloatSafe(src);
        return null == floatSafe?defaultValue:floatSafe;
    }



    public static Double getDouble(Object src){
        return TypeUtils.castToDouble(src);
    }

    public static Double getDoubleSafe(Object src){
        try {
            return TypeUtils.castToDouble(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static double getDoubleValue(Object src){
        return getDoubleValue(src,0D);
    }

    public static double getDoubleValue(Object src,double defaultValue){
        Double doubleSafe = getDoubleSafe(src);
        return null == doubleSafe?defaultValue:doubleSafe;
    }

    public static BigDecimal getBigDecimal(Object src){
        return TypeUtils.castToBigDecimal(src);
    }

    public static BigDecimal getBigDecimalSafe(Object src){
        try {
            return TypeUtils.castToBigDecimal(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static BigDecimal getBigDecimalSafe(Object src, BigDecimal defaultValue){
        try {
            return TypeUtils.castToBigDecimal(src);
        } catch (Exception e) {
            return defaultValue;
        }
    }


    public static BigInteger getBigInteger(Object src){
        return TypeUtils.castToBigInteger(src);
    }

    public static BigInteger getBigIntegerSafe(Object src){
        try {
            return TypeUtils.castToBigInteger(src);
        } catch (Exception e) {
            return null;
        }
    }

    public static BigInteger getBigIntegerSafe(Object src,BigInteger defaultValue){
        try {
            return TypeUtils.castToBigInteger(src);
        } catch (Exception e) {
            return defaultValue;
        }
    }
}
